package com.sinoglobal.ningxia.activity.mymessage;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.EnentItemActivity;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.adapter.MyMessageAdapter;
import com.sinoglobal.ningxia.beans.MessageVo;
import com.sinoglobal.ningxia.dialog.DialogBuilder;
import com.sinoglobal.ningxia.dialog.MDialogMethod;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;

/**
 * @author yrq
 * @createdate 2014-7-24 下午16:57:49
 * @Description:我的消息 列表
 */
public class MyMessageActivity extends BaseActivity {

    private ListView myMessageList;
    private MessageVo oldRresultMessageVo = new MessageVo();
    private MyMessageAdapter adapter;
    private RelativeLayout nomessage;
    private String imei;
    private String isMessagePush = "1"; // 消息推送
    private String isActivityPush = "1"; // 活动推送

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        templateButtonRight.setBackgroundResource(R.drawable.btn_small_selector);
        templateButtonRight.setGravity(Gravity.CENTER);
        templateButtonRight.setText(getString(R.string.clear_empty));
        titleView.setText(getString(R.string.myMessage));
        templateButtonRight.setTextColor(Color.WHITE);
        templateButtonRight.setVisibility(View.VISIBLE);
        templateButtonRight.setBackgroundResource(R.color.red600);
        setContentView(R.layout.activity_mymessage);
        openChenJInShi();//开启沉浸式
        adapter = new MyMessageAdapter(MyMessageActivity.this);
        init();
        showListener();
        getMessageFromNet();
    }

    private void init() {
        myMessageList = (ListView) findViewById(R.id.mymessage);
        myMessageList.setAdapter(adapter);
        nomessage = (RelativeLayout) findViewById(R.id.nomessage);
    }

    private void getMessageFromNet() {

        imei = FlyApplication.getUniquely(MyMessageActivity.this); // 获取手机唯一标识码
        if (SharedPreferenceUtil.getBoolean(FlyApplication.context, "isMessagePush")) {
            isMessagePush = "1";
        } else {
            isMessagePush = "2";
        }
        if (SharedPreferenceUtil.getBoolean(FlyApplication.context, "isActivityPush")) {
            isActivityPush = "1";
        } else {
            isActivityPush = "2";
        }
        /**
         * 若 消息推送和 活动推送都不推送则不访问网络 否则会访问接口
         */
        if ("2".equals(isMessagePush) && "2".equals(isActivityPush)) {
            getListFromSharePreference();
        } else {
            new ItktOtherAsyncTask<Void, Void, MessageVo>(false) {

                @Override
                public MessageVo before(Void... params) throws Exception {
                    // 接口
                    return RemoteImpl.getInstance().getMessageInfo(imei, isActivityPush, isMessagePush);
                    // return JSON.parseObject(TestJson.getMessageVo(),
                    // MessageVo.class);
                }

                @Override
                public void after(MessageVo result) {
                    MessageVo oldRresultMessageVo = new MessageVo();
                    String messageString = SharedPreferenceUtil.getString(getApplicationContext(), "messageVo");
                    if (messageString != null && messageString.length() > 0) {
                        oldRresultMessageVo = JSON.parseObject(messageString, MessageVo.class);
                    }
                    if (result.getList() != null && result.getList().size() != 0) {
                        oldRresultMessageVo.getList().addAll(0, result.getList());
                    }
                    SharedPreferenceUtil.saveString(getApplicationContext(), "messageVo", JSON.toJSONString(oldRresultMessageVo));
                    getListFromSharePreference(); // 从共享参数里面获取消息内容
                }

                @Override
                public void exception() {
                    getListFromSharePreference();
                }
            }.execute();
        }

    }

    /**
     * 从共享参数里面获取消息内容
     */
    private void getListFromSharePreference() {
        // 从共享参数里面获取消息内容
        String messageString = SharedPreferenceUtil.getString(getApplicationContext(), "messageVo");
        if (messageString != null) {
            try {
                oldRresultMessageVo = JSON.parseObject(messageString, MessageVo.class);
            } catch (Exception e) {
                oldRresultMessageVo = null;
            }
            if (oldRresultMessageVo != null && oldRresultMessageVo.getList() != null && oldRresultMessageVo.getList().size() > 0) {
                setAdapterTolist(oldRresultMessageVo);
            } else {
                nomessage.setVisibility(View.VISIBLE);
                myMessageList.setVisibility(View.GONE);
            }
        } else {
            nomessage.setVisibility(View.VISIBLE);
            myMessageList.setVisibility(View.GONE);
        }
    }

    /**
     * 将从共享参数里面获取的内容设置给列表适配器
     */
    private void setAdapterTolist(MessageVo result) {
        if (result != null && result.getList() != null && result.getList().size() != 0) {
            adapter.setList(result.getList());
        }
    }

    /**
     * 设置监听器
     */
    private void showListener() {
        myMessageList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 查看消息详情
                Intent intent = new Intent();
                if ("1".equals(adapter.getList().get(position).getType())) {
                    intent.setClass(MyMessageActivity.this, MessageContentActivity.class);
                    intent.putExtra("msgName", adapter.getList().get(position).getTitle());
                    intent.putExtra("msgTime", adapter.getList().get(position).getCreate_time());
                    intent.putExtra("msgContent", adapter.getList().get(position).getContent());
                    startActivity(intent);
                }
                if ("2".equals(adapter.getList().get(position).getType())) {
                    intent.setClass(MyMessageActivity.this, EnentItemActivity.class);
                    intent.putExtra("id", adapter.getList().get(position).getAct_id());
                    startActivity(intent);
                }
            }
        });
        templateButtonRight.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String messge = "";
                if (adapter.getList().size() == 0) {
                    messge = getString(R.string.no_message_cleared);
                    final DialogBuilder dialogBuilder = new DialogBuilder(MyMessageActivity.this);
                    dialogBuilder.Dialogbtn1(messge, R.layout.dialog_one_button, new MDialogMethod() {

                        @Override
                        public void dialogOk() {
                            dialogBuilder.mdialog.dismiss();
                        }

                        @Override
                        public void dialogCancel() {
                            dialogBuilder.mdialog.dismiss();
                        }
                    });

                } else {
                    messge = getString(R.string.sure_clear_message);
                    final DialogBuilder dialogBuilder = new DialogBuilder(MyMessageActivity.this);
                    dialogBuilder.Dialogbtn2(messge, R.layout.dialog_two_button, new MDialogMethod() {

                        @Override
                        public void dialogOk() {
                            SharedPreferenceUtil.removeString(getApplicationContext(), "messageVo");

                            nomessage.setVisibility(View.VISIBLE);
                            myMessageList.setVisibility(View.GONE);
                            dialogBuilder.mdialog.dismiss();
                            if (adapter != null && adapter.getList() != null)
                                adapter.getList().clear();
                        }

                        @Override
                        public void dialogCancel() {
                            dialogBuilder.mdialog.dismiss();
                        }
                    });
                }

            }
        });
    }

}
